package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.BaseInfoDto;
import com.itheima.ydd.dto.CateDto;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.dto.UserDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.mapper.YddArticleMapper;
import com.itheima.ydd.mapper.YddUserMapper;
import com.itheima.ydd.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements IIndexService {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private IYddUserService yddUserService;

    @Autowired
    private IYddTagService yddTagService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddArticleService yddArticleService;

    @Autowired
    private IYddCateService yddCateService;

    @Autowired
    private YddUserMapper yddUserMapper;

    @Autowired
    private YddArticleMapper articleMapper;

    @Override
    public R profile(Long userId) {
        //1.1 查询登录用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //1.2 填入用户角色信息
        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        String sysUserRoleId = sysUser.getRoleId();
        sysRoleWrapper.eq(SysRole::getId,sysUserRoleId.substring(2, sysUserRoleId.length() - 2));
//        sysRoleWrapper.select(SysRole::getName);
        //1.2.1 查出对应的角色详细信息
        SysRole sysRole = sysRoleService.getOne(sysRoleWrapper);
        //1.2.2 封装进sysUserDto对象中
        SysUserDto userInfo = new SysUserDto();
        BeanUtils.copyProperties(sysUser,userInfo);
       //userInfo.setRolename(sysRole.getName());

        //2 查菜单数据
        LambdaQueryWrapper<SysMenu> sysMenuWrapper = new LambdaQueryWrapper<>();
        sysMenuWrapper.ne(SysMenu::getPath,"");
        List<SysMenu> menuList = sysMenuService.list(sysMenuWrapper);

        //3 dictsList
        LambdaQueryWrapper<SysDict> sysDictWrapper = new LambdaQueryWrapper<>();
        sysDictWrapper.select(SysDict::getType);
        sysDictWrapper.groupBy(SysDict::getType);
        //3.1 分组查询所有dict_type
        List<Object> dictTypes = sysDictService.listObjs(sysDictWrapper);
        Map<String, Map<String,String>> dictsList = new HashMap<>();
        //3.2 遍历dictTypes ，查询对应的dict信息，存入dictsList中
        for (Object o : dictTypes) {
            Map<String, String> map = new HashMap<>();

            sysDictWrapper = new LambdaQueryWrapper<>();
            sysDictWrapper.eq(SysDict::getType,o);
            List<SysDict> sysDicts = sysDictService.list(sysDictWrapper);
            for (SysDict sysDict : sysDicts) {
                //3.3 将dict_value,dict_label存入集合
                map.put(sysDict.getValue(),sysDict.getLabel());
            }

            //3.4 将保存后的数据存入dictsList
            dictsList.put((String)o,map);
        }

        //4 准备 btnList
        Map<Integer,String> btnList = new HashMap<>();
        btnList.put(0,"*");

        //5 将所有查出的数据存入 R 中，返回
        R r = new R();
        r.putData("btnList",btnList);
        r.putData("dictsList",dictsList);
        r.putData("menuList",menuList);
        r.putData("userInfo",userInfo);

        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }

    @Override
    public R baseInfo() {
        BaseInfoDto baseInfoDto = new BaseInfoDto();

        List<YddCate> cateList = yddCateService.list();

        List<CateDto> collect = cateList.stream().map(item -> {
            CateDto cateDto = new CateDto();
            BeanUtils.copyProperties(item, cateDto);
            LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YddArticle::getCateid, item.getId());
            int num = yddArticleService.count(queryWrapper);
            cateDto.setNum(num);
            return cateDto;
        }).collect(Collectors.toList());

        baseInfoDto.setAllCateList(collect);

        baseInfoDto.setCateNum(yddCateService.count());
        //广告位
        baseInfoDto.setAdvposNum(yddAdvposService.count());
        //标签数量
        baseInfoDto.setTagNum(yddTagService.count());
        //管理员数量
        baseInfoDto.setAdminNum(sysUserService.count());
        //平台文章总数
        baseInfoDto.setArticleNum(yddArticleService.count());
        //广告图
        baseInfoDto.setCateNum(yddCateService.count());
        //广告位
        baseInfoDto.setAdvimgNum(yddAdvimgService.count());
        //最新注册的10个用户
        List<YddUser> list = yddUserMapper.list();
        baseInfoDto.setNewUserList(list);
        //热门文章

        List<YddArticle> articleList = articleMapper.getLise();
        baseInfoDto.setHotArticleList(articleList);

        //总用户

        int total =  yddUserMapper.selectTotal();
        int user_man = yddUserMapper.selectUser_man();
        int user_woman = yddUserMapper.selectuser_woman();
        UserDto userDto = new UserDto();
        userDto.setTotal(total);
        userDto.setUser_man(user_man);
        userDto.setUser_woman(user_woman);
        baseInfoDto.setUser(userDto);

        //return R.success("",baseInfoDto);

       /* LambdaQueryWrapper<YddArticle> articleQueryWrapper = new LambdaQueryWrapper<>();
        articleQueryWrapper.eq(YddArticle::getCateid,);


        LambdaQueryWrapper<CateDto> cateQueryWrapper = new LambdaQueryWrapper<>();
        cateQueryWrapper.select(YddCate::getId);
        cateQueryWrapper.select(YddCate::getCatename);




        baseInfoDto.setAdvposNum(yddAdvposService.count());
        baseInfoDto.setTagNum(yddTagService.count());
        baseInfoDto.setAdminNum(sysUserService.count());
        baseInfoDto.setArticleNum(yddArticleService.count());
        baseInfoDto.setCateNum(yddCateService.count());
        baseInfoDto.setAdvimgNum(yddAdvimgService.count());*/




        R r = new R();
        r.putData("all_cate",baseInfoDto.getAllCateList());
        r.putData("advpos_num",baseInfoDto.getAdvposNum());
        r.putData("tag_num",baseInfoDto.getTagNum());
        r.putData("admin_num",baseInfoDto.getAdminNum());
        r.putData("article_num",baseInfoDto.getArticleNum());
        r.putData("cate_num",baseInfoDto.getCateNum());
        r.putData("advimg_num",baseInfoDto.getAdvimgNum());
        r.putData("new_user",baseInfoDto.getNewUserList());
        r.putData("hot_article",baseInfoDto.getHotArticleList());
        r.putData("user",baseInfoDto.getUser());


        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }
}
